﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using olierFrm.GeneralCache;

namespace olierFrm.DataBase.FrameWork.Plugin
{
    public class DataWrapperLookup : DefinedSQL
    {

        public const string WrapperLookupCachePrefix = "WrapperLookup=";


        public DataWrapperLookup(string wrapperName)
        {
            string CacheKey = "WrapperLookup=" + wrapperName;
            if (!Cache.KeyExists(CacheKey))
            {
                WrapperLookups = (from d in base.DBEntity.WF_DataLinkItems
                                  where d.WrapperName == wrapperName
                                  select d).ToList<WF_DataLinkItems>();
                Cache.Add(CacheKey, WrapperLookups);
            }
            else
            {
                WrapperLookups = (List<WF_DataLinkItems>)Cache.Get(CacheKey);
            }
        }

        public DataWrapperLookup(string wrapperName, string lookupField)
        {
            string CacheKey = "WrapperLookup=" + wrapperName;
            if (!Cache.KeyExists(CacheKey))
            {
                List<WF_DataLinkItems> dwl = (from d in base.DBEntity.WF_DataLinkItems
                                              where d.WrapperName == wrapperName && d.LookupField == lookupField
                                              select d).ToList<WF_DataLinkItems>();
                WrapperLookups = dwl;
                Cache.Add(CacheKey, WrapperLookups);
            }
            else
            {
                this.WrapperLookups = (List<WF_DataLinkItems>)Cache.Get(CacheKey);
            }
        }

        public List<WF_DataLinkItems> FindByLookupField(string lookupField)
        {

            var dwl = from d in this.WrapperLookups
                      where d.LookupField == lookupField
                      select d;

            return dwl.ToList<WF_DataLinkItems>();

        }


        public List<string> GetKeyFields(WF_DataLinkItems dwl)
        {
            List<string> fields = new List<string>();
            if (dwl.KeyField != null && dwl.KeyField.Length != 0)
            {
                fields = dwl.KeyField.Split(new char[] { ';' }).ToList();
                fields.Remove("");
            }
            return fields;
        }

        public List<string> GetLookupFields(WF_DataLinkItems dwl)
        {
            List<string> fields = new List<string>();
            if (dwl.LookupField != null && dwl.LookupField.Length == 0)
            {
                fields = dwl.LookupField.Split(new char[] { ';' }).ToList();
                fields.Remove("");
            }
            return fields;
        }

        public Dictionary<string, string> GetFieldMatchs(WF_DataLinkItems dwl)
        {
            Dictionary<string, string> diFields = new Dictionary<string, string>();
            if (dwl.FieldMatchStr != null && dwl.FieldMatchStr.Length == 0)
            {
                var fields = dwl.FieldMatchStr.Split(new char[] { ';' }).ToList();
                fields.Remove("");
                foreach (var f in fields)
                {
                    var kv = f.Split('=');
                    diFields.Add(kv[0], kv[1]);
                }
            }
            return diFields;
        }
        public List<WF_DataLinkItems> WrapperLookups { get; set; }
    }

    public static class Extensions
    {
        /// <summary>
        /// 获取查询名称，共返回两个参数，1.Type；2.Query
        /// </summary>
        /// <param name="dwl"></param>
        /// <returns>参数1.Type=0，StdQuery；Type=1，DataObject。参数2.Query=查询名称</returns>
        public static int GetQuery(this WF_DataLinkItems dwl, out string Query)
        {

            if (dwl.StdQuery != null && dwl.StdQuery.Length != 0)
            {
                Query = dwl.StdQuery;
                return 0;
            }
            else if (dwl.DataObject != null && dwl.DataObject.Length != 0)
            {
                Query = dwl.DataObject;
                return 1;
            }
            else
                throw new Exception("SmartLookup，配置错误，请检查StdQuery或DataObject字段！");

        }
    }

}